﻿Imports HslCommunication
Imports HslCommunication.Profinet.Melsec
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Data

Public Class Form1
    Public adoConn As New ADODB.Connection
    Dim dtlhj As New DataTable
    Dim dt As New DataTable
    Dim fx3uPLC As MelsecA1ENet
    Dim plcConnectFLg As OperateResult
    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click

        If btnStart.Text = "连接设备" Then
            openlink()
        Else
            Closelink()
        End If


    End Sub
    Private Sub openlink()
        dt.Clear()

        If lblIP.Text = "" Then Exit Sub

        lblStatus.Text = "设备连接中..."
        lan1.LanternBackground = Color.Gray
        Me.Refresh()
        DataGridView2.Enabled = False

        DataGridView1.DataSource = dt
        Try
            fx3uPLC = New MelsecA1ENet
            fx3uPLC.IpAddress = lblIP.Text
            fx3uPLC.Port = 2000
            fx3uPLC.ConnectTimeOut = 60000
            fx3uPLC.SleepTime = 0

            If fx3uPLC.IpAddressPing = Net.NetworkInformation.IPStatus.Success Then
                lan1.LanternBackground = Color.LimeGreen
                plcConnectFLg = fx3uPLC.ConnectServer
                'plcConnectFLg = New OperateResult
                'plcConnectFLg.IsSuccess = True
                Me.Refresh()
                Timer1.Start()
                btnStart.Text = "关闭连接"
            Else
                lblStatus.Text = "Ping失败！"
                lan1.LanternBackground = Color.Gray
                DataGridView2.Enabled = True
            End If

        Catch ex As Exception
            '连接失败
            MsgBox("连接失败！")
            lan1.LanternBackground = Color.Gray
            lblStatus.Text = ""
            Timer1.Stop()
        End Try
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Me.MaximizeBox = False
        'Me.WindowState = FormWindowState.Maximized
        initLHJ()

        dt.Columns.Add("DTtime")
        dt.Columns.Add("WD1")
        dt.Columns.Add("WD2")
        dt.Columns.Add("WD3")
        dt.Columns.Add("WD4")
        dt.Columns.Add("WD5")
        dt.Columns.Add("WD6")
        dt.Columns.Add("WD7")
        dt.Columns.Add("WD8")
        dt.Columns.Add("WD9")
        dt.Columns.Add("WD10")
        dt.Columns.Add("WD11")
        dt.Columns.Add("WD12")
        dt.Columns.Add("YL1")
        dt.Columns.Add("YL2")
        dt.Columns.Add("LHZSJ")
        dt.Columns.Add("SYSJ")
        dt.Columns.Add("LHSC")
        dt.Columns.Add("JD")
        dt.Columns.Add("JDNSC")

    End Sub

    Private Sub initLHJ()
        dtlhj.Columns.Add("name")
        dtlhj.Columns.Add("val")
        getDataFMText("lhj.csv")
        DataGridView2.DataSource = dtlhj
    End Sub

    Private Sub Closelink()
        DataGridView2.Enabled = True

        lblStatus.Text = ""
        lan1.LanternBackground = Color.Gray
        If IsNothing(plcConnectFLg) = False AndAlso plcConnectFLg.IsSuccess = True Then
            fx3uPLC.ConnectClose()
            Timer1.Stop()
        End If
        btnStart.Text = "连接设备"
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Try

            If plcConnectFLg.IsSuccess = True Then
                Dim dr As DataRow = dt.Rows.Add()
                '读取寄存地址内的数据 BEGIN----------

                Dim _smyl As Short = fx3uPLC.ReadInt16("D90").Content
                System.Threading.Thread.Sleep(100)
                Dim plcData(13) As Double
                Dim plcDataRes As OperateResult(Of Short()) = fx3uPLC.ReadInt16("D500", 14)
                If plcDataRes.IsSuccess = True Then
                    lblStatus.Text = "连接成功！"
                    '温度14个，压力2个
                    For i As Integer = 0 To plcData.Length - 1
                        plcData(i) = plcDataRes.Content(i)
                    Next

                    'PLC中的20个温度
                    dr("DTtime") = Now()
                    dr("WD1") = toZero(plcData(0)) '上1
                    dr("WD2") = toZero(plcData(1)) '上2
                    dr("WD3") = toZero(plcData(2)) '上3
                    dr("WD4") = toZero(plcData(3)) '上4
                    dr("WD5") = toZero(plcData(4)) '下1
                    dr("WD6") = toZero(plcData(5)) '下2
                    dr("WD7") = toZero(plcData(6)) '下3
                    dr("WD8") = toZero(plcData(7)) '下4
                    dr("WD9") = toZero(plcData(8)) '中1
                    dr("WD10") = toZero(plcData(9)) '中2
                    dr("WD11") = toZero(plcData(10)) '中3
                    dr("WD12") = toZero(plcData(11)) '中4

                    'PLC中的压力
                    dr("YL1") = _smyl / 10

                    dr("YL2") = plcData(13) / 10
                End If
                System.Threading.Thread.Sleep(100)


                Dim plcDataRes1 As OperateResult(Of Short()) = fx3uPLC.ReadInt16("D253", 3)

                If plcDataRes1.IsSuccess = True Then
                    dr("LHZSJ") = plcDataRes1.Content(0)
                    dr("SYSJ") = plcDataRes1.Content(1)
                    dr("LHSC") = plcDataRes1.Content(2)
                Else
                    dr("LHZSJ") = "NULL"
                    dr("SYSJ") = "NULL"
                    dr("LHSC") = "NULL"
                End If
                System.Threading.Thread.Sleep(100)
                dr("JD") = fx3uPLC.ReadInt16("D18").Content

                System.Threading.Thread.Sleep(100)
                dr("JDNSC") = fx3uPLC.ReadInt16("D211").Content
                'Dim plcDataRes2 As OperateResult(Of Short) = fx3uPLC.ReadInt16("D18")

                'If plcDataRes2.IsSuccess = True Then
                '    dr("JD") = plcDataRes2.Content
                'Else
                '    dr("JD") = "NULL"
                'End If

                'Else
                '        lblStatus.Text = "数据读取失败！"
                '        lan1.LanternBackground = Color.MediumOrchid
                '        Timer1.Stop()
                '    End If
            End If

        Catch ex As Exception
            '连接失败
            MsgBox("连接失败！")
            lan1.LanternBackground = Color.Gray
            lblStatus.Text = ""
            Timer1.Stop()
        Finally
            If plcConnectFLg.IsSuccess = True Then
                fx3uPLC.ConnectClose()
            End If
        End Try
    End Sub

    Private Function toZero(data As Double) As Double
        If data = 255 Then
            Return 0
        Else
            Return data
        End If
    End Function
    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Closelink()
    End Sub

    Public Sub getDataFMText(ByVal strFileFullPath As String)
        Dim intFileRowNo As Integer = 0
        Try
            Using srFileTxt As StreamReader = New StreamReader(strFileFullPath)
                Dim strline As String
                Dim strn As String
                Dim strv As String
                Do
                    strline = srFileTxt.ReadLine()
                    If String.IsNullOrEmpty(strline) = False Then
                        strn = strline.Split(",")(0)
                        strv = strline.Split(",")(1)
                        dtlhj.Rows.Add(strn, strv)
                        intFileRowNo = intFileRowNo + 1
                    End If

                Loop Until strline Is Nothing
                srFileTxt.Close()
                For index = 0 To 10
                    dtlhj.Rows.Add("", "")
                Next
            End Using
        Catch exException As Exception
            MsgBox(exException.Message)
        End Try
    End Sub

    Private Sub DataGridView2_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView2.SelectionChanged
        If DataGridView2.SelectedRows.Count > 0 Then
            Label2.Text = DataGridView2.SelectedRows(0).Cells("Colname").Value.ToString()
            lblIP.Text = DataGridView2.SelectedRows(0).Cells("ColVal").Value.ToString()
        End If
    End Sub
    Dim WithEvents tt As New Timer
    Private Sub tt_Tick(sender As Object, e As EventArgs) Handles tt.Tick
        'Panel1.Visible = False
        tt.Stop()
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs)
        tt.Interval = 2000
        tt.Start()
        'Panel1.Visible = True
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) 
        'Timer1_Tick1()
    End Sub
End Class
